package com.iv.durian.cloud.api;

import com.iv.durian.cloud.JsonResult;
import com.iv.durian.cloud.Lesson;
import com.iv.durian.cloud.api.service.LessonService;
import com.iv.durian.cloud.vo.LessonVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;


/**
 * Created by zlw on 2018-06-30.
 */

@RestController
@RequestMapping("/api/lesson")
@Slf4j
@Api(value = "LessonApi" ,description = "LessonApi",tags={"lesson"} )
public class LessonApi {

    @Autowired
    private LessonService lessonService;

    /**
     * 通過id获取课程信息
     * @return
     */
    @ApiOperation(value = "通過id获取课程信息")
    @RequestMapping(value = "/getById/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<LessonVO>> get (
            @ApiParam(name = "lesson_id",value = "新課id",required = true) @RequestParam(value = "lesson_id") Integer lessonId ){
        JsonResult r = new JsonResult();
        try {
            Lesson lesson = lessonService.getLessonById(lessonId);
            if (lesson != null) {
                LessonVO lessonVO = LessonVO.builder()
                        .id(lesson.getId())
                        .name(lesson.getName())
                        .teacher(lesson.getTeacher())
                        .price(lesson.getPrice())
                        .maximum(lesson.getMaximum())
                        .source(lesson.getSource())
                        .stageMin(lesson.getStageMin())
                        .stageMax(lesson.getStageMax())
                        .status(lesson.getStatus())
                        .createTime(lesson.getCreateTime())
                        .updateTime(lesson.getUpdateTime()).build();
                r.ok(lessonVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("通過课程id查询信息失败",e);
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 通過name获取课程信息
     * @param name
     * @return
     */
    @ApiOperation(value = "通過name获取课程信息")
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public ResponseEntity<JsonResult<LessonVO>> getLessonByName (
            @RequestParam(value = "lesson_name") String name){
        JsonResult r = new JsonResult();
        try {
            Lesson lesson = lessonService.getLessonByName(name);
            if (lesson != null) {
                LessonVO lessonVO = LessonVO.builder()
                        .id(lesson.getId())
                        .name(lesson.getName())
                        .teacher(lesson.getTeacher())
                        .price(lesson.getPrice())
                        .maximum(lesson.getMaximum())
                        .source(lesson.getSource())
                        .stageMin(lesson.getStageMin())
                        .stageMax(lesson.getStageMax())
                        .status(lesson.getStatus())
                        .createTime(lesson.getCreateTime())
                        .updateTime(lesson.getUpdateTime()).build();
                r.ok(lessonVO);
            }
        } catch (Exception e) {
            r.error(e.getClass().getName() + ":" + e.getMessage());
            log.error("课程信息查询失败",e);
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 更新課程名稱
     * @param lessonId
     * @param newName
     */
    @ApiOperation(value = "更新課程名稱")
    @RequestMapping(value = "/changeName", method = RequestMethod.POST)
    public void changeLessonName(
            @RequestParam(value = "lesson_id") Integer lessonId,
            @ApiParam(name = "new_name",value = "新課程名",required = true) @RequestParam(value = "new_name") String newName ){
        Lesson lesson = lessonService.getLessonById(lessonId);
        Assert.notNull(lesson,"課程不存在");
        lessonService.updateLessonName(lesson.getId(),newName);
    }

    /**
     * 刪除某課程byId
     * @param lessonId
     */
    @ApiOperation(value = "刪除某課程byId")
    @RequestMapping(value = "/removeById", method = RequestMethod.POST)
    public void removeLessonById(
            @RequestParam(value = "lesson_id") Integer lessonId){
        lessonService.removeLessonById(lessonId);
    }

    @ApiOperation(value = "查詢課程總數")
    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public int getCount(@ApiParam(name = "status",value = "状态 0-维护 1-上线",required = true) @RequestParam(value = "status") Integer status){
        return lessonService.countLesson(status);
    }



    /**
     * 添加課程
     * @param name
     * @param price
     * @param maximum
     * @param source
     * @param stageMin
     * @param stageMax
     * @param status
     */
    @ApiOperation(value = "添加課程")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addLesson(
            @ApiParam(name = "name",value = "課程名",required = true) @RequestParam(value = "name") String name,
            @ApiParam(name = "price",value = "價格",required = true) @RequestParam(value = "price") String price,
            @ApiParam(name = "teacher",value = "教师名",required = true) @RequestParam(value = "teacher") String teacher,
            @ApiParam(name = "maximum",value = "一班最多学员",required = true) @RequestParam(value = "maximum") Integer maximum,
            @ApiParam(name = "source",value = "课程提供者 0-机构 1-个人",required = true) @RequestParam(value = "source") Integer source,
            @ApiParam(name = "stageMin",value = "年龄段-开始",required = true) @RequestParam(value = "stageMin") Integer stageMin,
            @ApiParam(name = "stageMax",value = "年龄段-結束",required = true) @RequestParam(value = "stageMax") Integer stageMax,
            @ApiParam(name = "status",value = "状态 0-维护 1-上线",required = true) @RequestParam(value = "status") Integer status){
        Lesson lesson = lessonService.getLessonByName(name);
        Assert.isNull(lesson,"該課程已存在");
        if (StringUtils.isEmpty(name)) {
            log.error("[插入的課程名為空]");
            return;
        }
        Lesson lesson02 = new Lesson();
        lesson02.setName(name);
        BigDecimal price02 = new BigDecimal(price);
        lesson02.setPrice(price02);
        lesson02.setMaximum(maximum);
        lesson02.setSource(source);
        lesson02.setTeacher(teacher);
        lesson02.setStageMin(stageMin);
        lesson02.setStageMax(stageMax);
        lesson02.setStatus(status);

        lessonService.addLesson(lesson02);
    }


    @ApiOperation(value = "羅列課程")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Lesson> list(
            @ApiParam(name = "status",value = "状态 0-维护 1-上线",required = true) @RequestParam(value = "status") Integer status,
            @ApiParam(name = "offset",value = "",required = true) @RequestParam(value = "offset") Integer offset,
            @ApiParam(name = "limit",value = "",required = true) @RequestParam(value = "limit") Integer limit){
        return lessonService.list(status,offset, limit);
    }



}
